<input class="content-warning-input"
       type="text"
       placeholder="{intl.contentWarning}"
       aria-label="{intl.contentWarning}"
       bind:value=rawText
/>
<style>
  .content-warning-input {
    font-size: 1.2em;
    margin: 10px 0 0 5px;
    padding: 10px;
    border: 1px solid var(--input-border);
    width: calc(100% - 5px);
  }
</style>
<script>
  import { store } from '../../_store/store.js'
  import { scheduleIdleTask } from '../../_utils/scheduleIdleTask.js'
  import { observe } from 'svelte-extras'
  import { throttleTimer } from '../../_utils/throttleTimer.js'

  const updateContentWarningInStore = throttleTimer(scheduleIdleTask)

  export default {
    oncreate () {
      this.setupSyncFromStore()
      this.setupSyncToStore()
    },
    store: () => store,
    data: () => ({
      rawText: ''
    }),
    methods: {
      observe,
      setupSyncFromStore () {
        this.observe('contentWarning', contentWarning => {
          this.set({ rawText: contentWarning })
        })
      },
      setupSyncToStore () {
        const { realm } = this.get()
        this.observe('rawText', rawText => {
          updateContentWarningInStore(() => {
            this.store.setComposeData(realm, {
              sensitive: !!rawText, // toggling the content warning automatically toggles sensitive media
              contentWarning: rawText
            })
            this.store.save()
          })
        }, { init: false })
      }
    }
  }
</script>
